import MiniFox from "@/util/minifox";


import http from "@/util/http";
import serviceConfig from "@/config/serviceConfig.json";

const service = (function () {
  let proxy = {};
  for (let i = 0; i < serviceConfig.adapters.length; i++) {
    let adapter = serviceConfig.adapters[i];
    let proxyItem = {};

    for (let n = 0; n < adapter.api.length; n++) {
      if (adapter.api[n].url)
        (function (adapter, api) {
          var baseUri;
          var url;
          if (api.url.indexOf("http://") === 0 || api.url.indexOf("https://") === 0) {
            baseUri = "";
            url = api.url.trim("/");
          }
          else {
            baseUri = adapter.host ? ((adapter.host.indexOf("http://") === 0 || adapter.host.indexOf("https://") === 0) ? adapter.host : process.env[adapter.host]) : "";
            baseUri = baseUri || (window.location.protocol + "//" + window.location.host);
            url = (adapter.virtualPath && adapter.virtualPath != "/") ? ("/" + adapter.virtualPath.trim("/") + "/" + api.url.trim("/")) : "/" + api.url.trim("/");
          }

          var method = api.method || serviceConfig.defaults.method;
          var withCredentials = api.enableCookies || serviceConfig.defaults.enableCookies;

          var autoMsg = api.autoMsg || serviceConfig.defaults.autoMsg;

          var timeOut = api.timeOut || serviceConfig.defaults.timeOut;
          var showProcessing = api.showProcessing;
          if (typeof showProcessing == 'undefined') {
            showProcessing = serviceConfig.defaults.showProcessing;
          }

          var requireToken = api.requireToken || serviceConfig.defaults.requireToken;

          var headers = serviceConfig.defaults.headers || {};
          if (api.headers)
            headers = Object.assign(headers, api.headers);
          var params = api.params || {};
          var data = api.data;

          if (api.noAjax) {
            var uri = new MiniFox.Uri(baseUri + url);
            //uri.addParams(api.params);
            var webRequest = {
              fullUri: uri,
              //baseUri: baseUri,
              url: url,
              method: method,
              headers: headers,
              params: params,
              data: data,
              toString: function () {
                this.fullUri.addParams(this.params);
                return this.fullUri.toString();
              },
            };
            proxyItem[api.name] = webRequest;
          } else {
            var func = function (request) {
              request = request || {};
              var requestUrl = url;
              var postData;
              if (data) {
                postData = Object.assign(data, request.data);
              } else {
                postData = request.data;
              }

              if (request && request.routeData) {
                for (var key in request.routeData) {
                  var parkey = `{${key}}`;
                  requestUrl = requestUrl.replace(
                    parkey,
                    request.routeData[key]
                  );
                }
              }
              if (request && request.method) {
                method = request.method;
              }

              var config = {
                baseURL: baseUri,
                url: requestUrl,
                method: method,
                headers: headers,
                withCredentials: withCredentials,
                requireToken: requireToken,
                showProcessing: showProcessing,
                data: postData,
                params: Object.assign(params, request.params),
                timeout: timeOut,
                autoMsg: autoMsg,
                nowrap: api.nowrap,
                handleBlob: api.handleBlob,
                responseType: api.responseType || "json",
              };
              if (
                request.onUploadProgress &&
                typeof request.onUploadProgress == "function"
              ) {
                config.onUploadProgress = request.onUploadProgress;
              }
              if (
                request.onDownloadProgress &&
                typeof request.onDownloadProgress == "function"
              ) {
                config.onDownloadProgress = request.onDownloadProgress;
              }
              if (request.transformRequest)
                config.transformRequest = request.transformRequest;

              return http(config);
              // }
            };
            func.baseUri = baseUri;
            func.url = url;
            func.method = method;
            func.headers = headers;
            func.params = params;
            func.data = data;

            proxyItem[api.name] = func;
          }
        })(adapter, adapter.api[n]);
    }

    proxy[adapter.name] = proxyItem;
  }
  return proxy;
})();

export default service;
